﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
//
using PerceptiveMCAPI;
using PerceptiveMCAPI.Types;

namespace PerceptiveMCAPI_Test
{
    public partial class show_getAccountDetails : System.Web.UI.UserControl
    {
        public void Display ( getAccountDetailsOutput output )
        {
            this.Visible = true;
            // throw it into a collection to bind
            List<getAccountDetailsResults> results = new List<getAccountDetailsResults>();
            results.Add( output.result );
            //
            dvAccountDetails.DataSource = results;
            dvAccountDetails.DataBind();
        }

        protected string FormatContactInfo ( object contact_info )
        {
            getAccountDetails_contact ctc = (getAccountDetails_contact)contact_info;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat( "Name: {0}<br />", ApiHelper.stringNameFML( ctc.fname, ctc.lname ) );
            sb.AppendFormat( "Company: {0}<br />", ctc.company );
            sb.AppendFormat( "Address: {0}<br />",
               ApiHelper.stringAddress( ctc.address1, ctc.address2, ctc.city, ctc.state, ctc.zip, ctc.country ) );
            sb.AppendFormat( "Email: {0}<br />", ctc.email );
            sb.AppendFormat( "Url: {0}", ctc.url );

            return sb.ToString();
        }
        protected string FormatModulesInfo ( object modules_info )
        {
            List<getAccountDetails_module> modules = (List<getAccountDetails_module>)modules_info;
            if ( modules.Count == 0 )
            {
                return "No modules found";
            }
            // date order
            IOrderedEnumerable<getAccountDetails_module> sorted = modules.OrderBy( s => s.added );
            StringBuilder sb = new StringBuilder();
            foreach ( getAccountDetails_module mod in sorted )
            {
                string added_date = ( mod.added == DateTime.MinValue ) ? string.Empty : mod.added.ToString( "g" );
                sb.AppendFormat( "{0} added {1}<br />", mod.name, added_date );
            }
            return sb.ToString();
        }
        protected string FormatOrdersInfo ( object orders_info )
        {
            List<getAccountDetails_order> orders = (List<getAccountDetails_order>)orders_info;
            if ( orders.Count == 0 )
            {
                return "No orders on file";
            }
            IOrderedEnumerable<getAccountDetails_order> sorted = orders.OrderBy( s => s.order_id );
            StringBuilder sb = new StringBuilder();
            sb.Append( "<table style='width: auto;' cellpadding='4' border='1'><tr><th>Order Id</th><th>Type</th><th>Amount</th><th>Date</th><th>Credits User</th></tr>" );
            foreach ( getAccountDetails_order order in sorted )
            {
                string order_date = ( order.order_date == DateTime.MinValue ) ? string.Empty : order.order_date.ToString( "g" );
                sb.AppendFormat( "<tr><td>{0}</td><td>{1}</td><td align='right'>{2}</td><td>{3}</td><td align='right'>{4}</td></tr>",
                   order.order_id, order.type, order.amount, order_date, order.credits_user );
            }
            sb.Append( "</table>" );
            return sb.ToString();
        }
        // ---- rewards
        protected string FormatRewardsInfo ( object rewards_info )
        {
            if ( rewards_info == null || rewards_info as getAccountDetails_rewards == null )
            {
                return "No rewards information found";
            }
            getAccountDetails_rewards rewards = (getAccountDetails_rewards)rewards_info;

            StringBuilder sb = new StringBuilder();
            sb.Append( "<table style='width: auto;' cellpadding='4' border='1'>" );
            sb.AppendFormat( "<tr><td>referals_this_month</td><td>{0}</td></tr>", rewards.referals_this_month );
            sb.AppendFormat( "<tr><td>notify_on</td><td>{0}</td></tr>", rewards.notify_on );
            sb.AppendFormat( "<tr><td>notify_email</td><td>{0}</td></tr>", rewards.notify_email );
            sb.AppendFormat( "<tr><td>credits</td><td>this_month: {0}, total_earned: {1}, remaining: {2}</td></tr>",
               rewards.credits.this_month, rewards.credits.total_earned, rewards.credits.remaining );
            sb.AppendFormat( "<tr><td>inspections</td><td>this_month: {0}, total_earned: {1}, remaining: {2}</td></tr>",
               rewards.inspections.this_month, rewards.inspections.total_earned, rewards.inspections.remaining );
            if ( rewards.referrals.Count == 0 )
            {
                sb.Append( "<tr><td>referrals</td><td>** No referrals found **</td></tr>" );
            }
            else
            {
                foreach ( getAccountDetails_rewards.reward_referral referral in rewards.referrals )
                {
                    sb.AppendFormat( "<tr><td>referral</td><td>name: {0}, email: {1}<br />signup_date: {2}, type: {3}</td></tr>",
                       referral.name, referral.email, referral.signup_date, referral.type );
                }
            }
            if ( rewards.applied.Count == 0 )
            {
                sb.Append( "<tr><td>applied</td><td>** No applied found **</td></tr>" );
            }
            else
            {
                foreach ( getAccountDetails_rewards.reward_applied applied in rewards.applied )
                {
                    sb.AppendFormat( "<tr><td>applied</td><td>'applied' info here - dummmy date: {0}</td></tr>",
                        DateTime.Now );
                }
            }
            sb.Append( "</table>" );
            return sb.ToString();
        }
    }
}